package Model;

/*CREATE  TABLE IF NOT EXISTS `mydb`.`Reserva` (
  `idReserva` INT NOT NULL AUTO_INCREMENT ,
  `data` DATETIME NOT NULL ,
  `Turnos_idTurnos` INT NOT NULL ,
  `Laboratorio_idLaboratorio` INT NOT NULL ,
  PRIMARY KEY (`idReserva`) ,
  INDEX `fk_Reservas_Turnos1` (`Turnos_idTurnos` ASC) ,
  INDEX `fk_Reserva_Laboratorio1` (`Laboratorio_idLaboratorio` ASC) ,
  CONSTRAINT `fk_Reservas_Turnos1`
    FOREIGN KEY (`Turnos_idTurnos` )
    REFERENCES `mydb`.`Turnos` (`idTurnos` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Reserva_Laboratorio1`
    FOREIGN KEY (`Laboratorio_idLaboratorio` )
    REFERENCES `mydb`.`Laboratorio` (`idLaboratorio` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;*/
public class Reserva {
    //<editor-fold defaultstate="collapsed" desc="Connection">
    private static com.mysql.jdbc.Connection connection;

    public static com.mysql.jdbc.Connection getConnection() {
        return connection;
    }

    public static void setConnection(com.mysql.jdbc.Connection connection) {
        Reserva.connection = connection;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Variable Declaration">
    private int idReserva;
    private java.util.Date data;
    private Turnos Turnos_idTurnos;
    private Laboratorio Laboratorio_idLaboratorio;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public Reserva(int idReserva) {
        try {
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery("SELECT * FROM itens WHERE idReserva = '" + idReserva + "'");
            rs.next();
            this.idReserva = rs.getInt("idReserva");
            this.data = rs.getDate("data");
            this.Turnos_idTurnos = new Turnos(rs.getInt("Turnos_idTurnos"));
            this.Laboratorio_idLaboratorio = new Laboratorio(rs.getInt("Laboratorio_idLaboratorio"));
        } catch (Exception e) {
        }
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="toString">
    @Override
    public String toString() {
        String s="Reserva:";
            s += "idReserva=" + idReserva;
        return s;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Getter">
    public int idReserva() {
        return this.idReserva;
    }

    public java.util.Date data() {
        return this.data;
    }

    public Turnos Turnos_idTurnos() {
        return this.Turnos_idTurnos;
    }

    public Laboratorio Laboratorio_idLaboratorio() {
        return this.Laboratorio_idLaboratorio;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Setter">
    public boolean idReserva(int idReserva) {
        boolean ok = update("idReserva", idReserva);
        if (ok) {
            this.idReserva = idReserva;
        }
        return ok;
    }
    public boolean data(java.util.Date data) {
        boolean ok = update("data", data);
        if (ok) {
            this.data = data;
        }
        return ok;
    }
    public boolean Turnos_idTurnos(Turnos Turnos_idTurnos) {
        boolean ok = update("Turnos_idTurnos", Turnos_idTurnos.idTurnos());
        if (ok) {
            this.Turnos_idTurnos = Turnos_idTurnos;
        }
        return ok;
    }
    public boolean Laboratorio_idLaboratorio(Laboratorio Laboratorio_idLaboratorio) {
        boolean ok = update("Laboratorio_idLaboratorio", Laboratorio_idLaboratorio.idLaboratorio());
        if (ok) {
            this.Laboratorio_idLaboratorio = Laboratorio_idLaboratorio;
        }
        return ok;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Update">
    /**
     *
     * @param campo
     * @param valor
     * @return
     */
    public boolean update(String campo, Object valor) {
        try {
            String sql = "UPDATE Reserva \n"
                    + "SET " + campo + " = '";
            if (valor instanceof String) {
                sql += (String) valor;
            } else if (valor instanceof Integer) {
                sql += (Integer) valor;
            } else if (valor instanceof java.sql.Timestamp) {
                sql += ((java.sql.Timestamp) valor).toString().split("\\.")[0];
            } else if (valor instanceof java.util.Date) {
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ss");
                sql += sdf.format((java.util.Date) valor);
            } else {
                sql += valor.toString();
            }
            sql += "'\n WHERE \n idReserva = '" + idReserva + "'";
            com.mysql.jdbc.Statement st =
                    (com.mysql.jdbc.Statement) connection.createStatement();
            int x = st.executeUpdate(sql);
            if (x == 1) {
                return true;
            }
        } catch (Exception ex) {
        }
        return false;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Search">
    public static java.util.ArrayList<Reserva> searchIdReserva(int valor) {
        return search("idReserva", valor+"");
    }
    public static java.util.ArrayList<Reserva> searchData(java.util.Date valor) {
        return search("data", valor+"");
    }
    public static java.util.ArrayList<Reserva> searchTurnos_idTurnos(Turnos valor) {
        return search("Turnos_idTurnos", valor.idTurnos()+"");
    }
    public static java.util.ArrayList<Reserva> searchLaboratorio_idLaboratorio(Laboratorio valor) {
        return search("Laboratorio_idLaboratorio", valor.idLaboratorio()+"");
    }

    public static java.util.ArrayList<Reserva> search(String campo, String valor) {
        try {
            java.util.ArrayList<Reserva> retorno = new java.util.ArrayList<>();
            String sql = "SELECT idReserva FROM Reserva "
                    + "WHERE " + campo + " LIKE '%" + valor + "%' "
                    + "ORDER BY " + campo;
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery(sql);
            while (rs.next()) {
                retorno.add(new Reserva(rs.getInt("idReserva")));
            }
            return retorno;
        } catch (Exception e) {
        }
        return null;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="New">
    public static Reserva newReserva(int idReserva, java.util.Date data, Turnos Turnos_idTurnos, Laboratorio Laboratorio_idLaboratorio){
            String sql = "INSERT INTO Reserva (idReserva, data, Turnos_idTurnos, Laboratorio_idLaboratorio)"
                    + " VALUES "
                    + "('"+idReserva+"', '"+(new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format((java.util.Date) data)+"', '"+Turnos_idTurnos.idTurnos() + "', '"+Laboratorio_idLaboratorio.idLaboratorio() + "');";
        com.mysql.jdbc.Statement stmt = null;
        com.mysql.jdbc.ResultSet rs = null;
        try {
            stmt = (com.mysql.jdbc.Statement) connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                    java.sql.ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate(sql, com.mysql.jdbc.Statement.RETURN_GENERATED_KEYS);
            rs = (com.mysql.jdbc.ResultSet) stmt.getGeneratedKeys();
            if (rs.next()) {
                return new Reserva(rs.getInt("idReserva"));
            } else {
                return new Reserva(idReserva);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
        return null;
    }
    //</editor-fold>
}